using System.Collections.Generic;

// 气象站类（被观察者）

public class WeatherStation

{

    private DateTime lastCollectTime;

    private double temperature;

    private double humidity;

    private List<IObserver> observers = new List<IObserver>();

    // 属性（设置时触发通知）

    public DateTime LastCollectTime

    {

        get { return lastCollectTime; }

        set

        {

            lastCollectTime = value;

            NotifyObservers();

        }

    }

    public double Temperature

    {

        get { return temperature; }

        set

        {

            temperature = value;

            NotifyObservers();

        }

    }

    public double Humidity

    {

        get { return humidity; }

        set

        {

            humidity = value;

            NotifyObservers();

        }

    }

    // 注册观察者

    public void RegisterObserver(IObserver observer)

    {

        observers.Add(observer);

    }

    // 通知所有观察者

    public void NotifyObservers()

    {

        foreach (var observer in observers)

        {

            observer.Update(this);

        }

    }

}

// 观察者接口

public interface IObserver

{

    void Update(WeatherStation station);

}

// 屏幕显示类（观察者）

public class ScreenDisplay : IObserver

{

    public void Update(WeatherStation station)

    {

        Console.WriteLine($"屏幕更新：时间={station.LastCollectTime}，温度={station.Temperature}°C，湿度={station.Humidity}%");

    }

}

// 数据库保存类（观察者）

public class DatabaseSaver : IObserver

{

    public void Update(WeatherStation station)

    {

        Console.WriteLine($"数据库保存：时间={station.LastCollectTime}，温度={station.Temperature}，湿度={station.Humidity}");

        // 实际项目中可添加数据库存储逻辑

    }

}

// 测试类

class Program

{

    static void Main()

    {

        WeatherStation station = new WeatherStation();

        ScreenDisplay screen = new ScreenDisplay();

        DatabaseSaver dbSaver = new DatabaseSaver();

        station.RegisterObserver(screen);

        station.RegisterObserver(dbSaver);

        // 模拟数据变化

        station.LastCollectTime = DateTime.Now;

        station.Temperature = 25.5;

        station.Humidity = 60;

        // 再次变化

        station.LastCollectTime = DateTime.Now.AddMinutes(10);

        station.Temperature = 26.0;

        station.Humidity = 58;

    }

}


